<?php
class upload extends SDO_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model("Events");
	}

	function uploadAttachment()
	{
		$eventId = intval($this->getpara('eventId'));
		if(!$eventId)
		{
			die('不存在该事件');
		}
		$attachment_isreply	= intval($this->getpara("attachment_isreply", 2));
		$attachment_isreply!=1 && $attachment_isreply=2;
		$this->load->library('upload');
		$file_path = UPLOADPATH.date('Y/m/d/');
		file_helper::createFolder($file_path);
		$config['upload_path'] = $file_path;
		$result = $this->upload->do_upload('Filedata',$config);
		$fileData = $this->upload->data();
		$this->load->model('Events');
		$file_true_name = strip_tags($_FILES['Filedata']['name']);
		$attachId = $this->Events->addAttachment($eventId, 'username', $file_path.$fileData['file_name'], $file_true_name, array('attachment_isreply' => $attachment_isreply));
		if(!$attachId)
		{
			die('入库失败');
		}
		echo json_encode(array('attachId'=>$attachId,'filePath'=>$file_path.$fileData['file_name'],'fileName'=>$file_true_name));
	}

	function delAttachment()
	{
		$aid = intval($this->getpara('aid'));
		$this->load->model('Events');
		if($this->Events->deleteAttachmentById($aid))
		{
			echo 'success';
		}
		else
		{
			echo 'failed';
		}
		exit;
	}

}
